#!/bin/bash
#(c) Copyright Barry Kauler June 2009
#Woof has various Puppy db files, Packages-puppy-*-official
#this script synchronises them all, including with the files at ibiblio.org...
#111129 recommend not update db files with online entries.
#121102 pet db file not only, use local file only.
#-- this script executed in the main woof dir... --

#db entry format: pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|
#ex: abiword-1.2.4|abiword|1.2.4|5|Document|999K|slackware/ab|abiword-1.2.4-5-i486.tgz|+aiksausus,+gtk2|a nice wordprocessor|
#optionally on the end: compileddistro|compiledrelease|repo| (fields 11,12,13)
#ex: slackware|12.2|official|

export LANG=C

[ ! -f ./DISTRO_PET_REPOS ] && exit 1
[ ! -f ./DISTRO_SPECS ] && exit 1

. ./_00func
. ./DISTRO_SPECS #has DISTRO_BINARY_COMPAT, DISTRO_COMPAT_VERSION
. ./DISTRO_PET_REPOS
source_compat_repos  # ./DISTRO_COMPAT_REPOS-
source_pkgs_specs    # ./DISTRO_PKGS_SPECS-

#find out if internet working...
PKG_DOCS_PET_REPOS=$(echo "$PKG_DOCS_PET_REPOS" | tr ' ' '\n')
ONE_PET_REPO="`echo "$PKG_DOCS_PET_REPOS" | head -n 1`"
ONE_PET_SITE="`echo -n "$ONE_PET_REPO" | cut -f 2 -d '|' | cut -f 3 -d '/'`"

#internet connected, check each of Packages-puppy-[2345]-official local against same file at ibiblio...
for ONEREPO in `find . -mindepth 1 -maxdepth 1 -name 'Packages-puppy-*-official' | sed 's|.*/||'`
do
   REMOTEREPO="`echo "$PKG_DOCS_PET_REPOS" | grep "/${ONEREPO}|" | head -n 1 | cut -f 2 -d '|'`"
   [ "$REMOTEREPO" = "" ] && continue #not an error. just not interested in this db file.
   echo "$PKGS_DOCS_PET_REPOS_LOCAL_ONLY" | grep -q "^${ONEREPO}$" && continue
   echo
   echo "Downloading ${ONEREPO}..."
   rm -f remotedbfile
   wget --output-document=remotedbfile $REMOTEREPO
   if [ $? -ne 0 ];then
    if [ -f ./$ONEREPO ];then #121102
     echo
     echo "ERROR downloading ${ONEREPO}, using local file only."
     echo -n "Press ENTER to continue: "
     read keepgoing
     continue
    else
     echo
     echo "ERROR downloading ${ONEREPO}, quiting."
     exit 1
    fi
   fi
   DBDIFFS="`diff $ONEREPO remotedbfile`"
   if [ "$DBDIFFS" = "" ];then
    echo "...files are the same."
    continue
   else
    NEWREMOTEPKGS="`echo "$DBDIFFS" | grep '^> ' | sed -e 's%^> %%'`"
    if [ "$NEWREMOTEPKGS" != "" ];then
     echo; echo #clear #echo
     echo "The remote $ONEREPO file has these new entries:"
     echo -e -n "\\033[1;31m" #red
     echo "$NEWREMOTEPKGS"
     echo -e -n "\\033[0;39m" #back to black.
     echo
     echo "Press any printable char then ENTER only to append these to local $ONEREPO"
     echo "or press ENTER only to ignore them. If using Woof for first time,"
     echo "recommend press ENTER only. Online files modified by other developers"
     echo "may introduce unexpected effects to a build."
     echo -n "Recommend press ENTER only: "
     read ignorethem
     if [ "$ignorethem" != "" ];then #111129 reverse logic.
      echo "...adding new packages."
      echo "$NEWREMOTEPKGS" >> $ONEREPO
     fi
    fi
    NEWLOCALPKGS="`echo "$DBDIFFS" | grep '^< ' | sed -e 's%^< %%'`"
    if [ "$NEWLOCALPKGS" != "" ];then
     echo; echo #clear #echo
     echo "WARNING, local $ONEREPO has these entries that are"
     echo "not in the remote db file:"
     echo -e -n "\\033[1;31m" #red
     echo "$NEWLOCALPKGS"
     echo -e -n "\\033[0;39m" #back to black.
     echo
     echo "...this is a matter for the system admin of remote site."
     echo "Note, this script will also check availability of pkgs on remote site and"
     echo "optionally delete invalid entries in local $ONEREPO"
     echo -n "Press ENTER to continue: "
     read keepongoing
    fi
   fi
   rm -f remotedbfile
done

### END ###
